<template>
  <div class="head">
    <span class="back-btn" @click="goHome" v-if="!isHome"><i class="el-icon-s-home"></i>首页</span>
    <div class="weather">
      <span class="v-line">{{ weather.weather }}</span>
      <span>当前气温</span>
      <span>{{ weather.temperature || 0 }}℃</span>
      <span>风速</span>
      <span>{{ weather.winddirection }}{{ weather.windpower }}</span>
    </div>
    <h2 class="title">{{ systemConfig['医院名称'] }}{{ title }}</h2>
    <div class="date">
      <span class="v-line">{{ date | date("HH:mm:ss") }}</span>
      <span>{{ date | date("yyyy-MM-dd") }}</span>
      <span @dblclick.stop="goSetConfig">{{ date | week }}</span>
    </div>
    <span class="out-btn" @click="outLogin"><i class="iconfont icon-guanji"></i></span>
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      default: "一站式医辅服务平台"
    },
    isHome: {
      type: Boolean,
      default: false,
    }
  },
  data() {
    return {
      weather: {},
      date: new Date().getTime(),
      dateTimer: null,
      systemConfig: {
        "医院名称": "成都市第七人民医院",
      },
    }
  },
  created() {
    this.weather = this.func.getSession("weather") || {};
    // this.systemConfig = this.func.getSession("systemConfig") || {};
  },
  mounted() {
    this.dateTimer = setInterval(() => {
      this.date = new Date().getTime();
    }, 1000);

    const m = new Date().getMinutes(), s = new Date().getSeconds();
    if (!this.weather.infocode || (m == 0 && s == 0)) {
      this.getWeather();
    }
    // this.getSystemConfig();
  },
  beforeDestroy() {
    clearInterval(this.dateTimer);
  },
  methods: {
    getWeather() {
      this.https("get", "/api/Values/GetWeather", { city: 510100, extensions: "base" }).then((res) => {
        if (res.lives.length > 0) {
          this.weather = res.lives[0];
          this.func.setSession("weather", this.weather)
        }
      });
    },
    getSystemConfig() {
      this.https("get", "/api/Values/ReadData", { "classKey": "config" }).then((res) => {
        const data = res.values;
        if (data) {
          this.systemConfig = JSON.parse(data);
        } else {
          this.systemConfig = {};
        }
        this.func.setSession("systemConfig", this.systemConfig)
      });
    },
    goSetConfig() {
      window.open(`/#/setting`);
    },
    goHome() {
      if (this.$route.path != '/monitor') {
        this.$router.push({
          path: "/monitor",
        })
      } else {
        this.$bus.$emit("Turn_Home");
      }
    },
    outLogin() {
      this.$confirm('即将注销登录退出到登录页面, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.func.removeSession("userInfo");
        this.$router.push({
          path: "/setting/login",
        })
      })
    }
  }
};
</script>
<style lang="scss" scoped>
.back-btn {
  background-color: transparent;
  border: 1px solid #0353A9;
  padding: 0px 1vw;
  height: 3vh;
  font-size: 0.68vw;
  border-radius: 2px;
  display: inline-flex !important;
  align-items: center;
  color: #01CAFF;
  position: absolute;
  left: 14vw;
  top: 1.5vh;
  z-index: 1;
  font-weight: bold;

  i {
    margin-right: 4px;
    font-size: 14px;
  }

  &:hover {
    cursor: pointer;
    opacity: 0.9;
  }
}

.out-btn {
  position: absolute;
  right: 1vw;
  left: initial;
  top: 1vh;
  color: #01CAFF;
  font-size: 0.8vw;

  &:hover {
    cursor: pointer;
    opacity: 0.8;
  }
}
</style>